package leetcode;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2022/3/1
 * Time:19:50
 */
public class deleteArrayRepeatNumsII {

    public static void main(String[] args) {
        int[] arr = {1,1,1,2,2,3,4};
        System.out.println(removeDuplicates(arr));
    }
    public static int removeDuplicates(int[] nums) {
        int len = nums.length;
        if(len < 2) return len;
        int j = 0;
        int count = 1;
        for(int i = 1;i<len;i++){
            if(nums[i] != nums[j]) {
                nums[++j] = nums[i];
                count = 1;
            }
            else if( nums[i] == nums[j] && count < 2 ){
                nums[++j] = nums[i];
                count++;
            }
        }
        return j + 1;
    }

    public int removeduplicates(int[] nums) {
        if(nums.length <= 2) return nums.length;

        int index = 2;
        for(int i = 2; i < nums.length; i++){
            if(nums[i] != nums[index-2])
                nums[index++] = nums[i];
        }

        return index;
    }
}
